package org.maven.dependencybrowser.implementations.zip;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.maven.dependencybrowser.controller.DependencyResolutionException;
import org.maven.dependencybrowser.implementations.Repository;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZipFileRepository implements Repository, Contextualizable
{

    public static final String KEY_ZIPFILE_PATH = "zip.filepath";
    public static final String PREFIX_REPOSITORY = "repository";

    private File repositoryZipFile;

    public Model resolveFile(String groupId, String artifactId, String version)
    {
        try
        {
            String path = PREFIX_REPOSITORY + "/" + groupId.replaceAll("\\.", "/") + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".pom";
            ZipFile zipFile = new ZipFile(repositoryZipFile);
            ZipEntry entry = zipFile.getEntry(path);
            if (entry == null)
            {
                return null;
            }

            MavenXpp3Reader pomReader = new MavenXpp3Reader();

            return pomReader.read(new InputStreamReader(new BufferedInputStream(zipFile.getInputStream(entry))));
        }
        catch (FileNotFoundException e)
        {
            throw new DependencyResolutionException(e);
        }
        catch (IOException e)
        {
            throw new DependencyResolutionException(e);
        }
        catch (XmlPullParserException e)
        {
            throw new DependencyResolutionException(e);
        }
    }

    public boolean canCreateSnapshot()
    {
        return false;
    }

    public void contextualize(Context context) throws ContextException
    {
        String zipFilePath = (String) context.get(KEY_ZIPFILE_PATH);
        if (zipFilePath == null || zipFilePath.trim().length() == 0)
        {
            throw new ContextException("Missing configuration parameter: " + KEY_ZIPFILE_PATH);
        }
        this.repositoryZipFile = new File(zipFilePath);
    }

}
